// -------------------------------------------------------
// Copyright (c) Wild West Games 2006, All Rights Reserved
// -------------------------------------------------------

#include "cbase.h"
#include "weapon_scoped.h"
#include "in_buttons.h"

IMPLEMENT_NETWORKCLASS_ALIASED( WeaponScoped, DT_WeaponScoped )

BEGIN_NETWORK_TABLE( CWeaponScoped, DT_WeaponScoped )
#ifdef CLIENT_DLL
RecvPropBool( RECVINFO( m_bZooming ) )
#else
SendPropBool( SENDINFO( m_bZooming ) )
#endif
END_NETWORK_TABLE()

CWeaponScoped::CWeaponScoped()
{
	m_bZooming = false;
}

#ifndef CLIENT_DLL

/**
* Allow zoom toggling whilst reloading
*/
void CWeaponScoped::ItemBusyFrame( void )
{
	CheckZoomToggle();
	BaseClass::ItemBusyFrame();
} 


/**
* Normal think cycle - check if we should zoom/unzoom
*/
void CWeaponScoped::ItemPostFrame( void )
{
	CheckZoomToggle();
	BaseClass::ItemPostFrame();
} 


/**
* Unzoom whilst weapon is reloading
*/
bool CWeaponScoped::Reload( void )
{
	if (BaseClass::Reload())
	{
		if (IsZooming())
		{
			Unzoom();
		}
		return true;
	}
	else
	{
		return false;
	}
}


/** 
* Unzoom when holstering the weapon
*/
bool CWeaponScoped::Holster( CBaseCombatWeapon *pSwitchingTo )
{ 
	if (BaseClass::Holster(pSwitchingTo))
	{
		if (IsZooming())
		{
			Unzoom();
		}
		return true;
	}
	else
	{
		return false;
	}
}


/**
* If the player has pressed the desired button, toggle zoom for this weapon
*/
void CWeaponScoped::CheckZoomToggle( void )
{
	CBasePlayer *pPlayer = ToBasePlayer(GetOwner());

	if ( pPlayer->m_afButtonPressed & IN_ATTACK2 )
	{
		ToggleZoom();
	}
}


/**
* If zooming, un-zoom; otherwise start zooming
*/
void CWeaponScoped::ToggleZoom( void )
{
	if (IsZooming())
	{
		Unzoom();
	}
	else
	{
		Zoom();
	}
}


/**
* Set the weapon's state to indicate zooming and send a
* message to the HUD to show the corresponding scope
*/
void CWeaponScoped::Zoom( void )
{
	CBasePlayer *pPlayer = ToBasePlayer(GetOwner());
	if (pPlayer && pPlayer->SetFOV(this, GetZoomFOV()))
	{
		m_bZooming = true;
		CSingleUserRecipientFilter filter(pPlayer);
		UserMessageBegin(filter, "ShowScope");
			WRITE_BYTE(GetScopeType());
		MessageEnd();
	}
}


/**
* Set the weapon's state to indicate no longer zooming and send a
* message to the HUD to hide the corresponding scope, if it is being shown
*/
void CWeaponScoped::Unzoom( void )
{
	CBasePlayer *pPlayer = ToBasePlayer(GetOwner());
	if (pPlayer && pPlayer->SetFOV(this, 0))
	{
		m_bZooming = false;
		CSingleUserRecipientFilter filter(pPlayer);
		UserMessageBegin(filter, "HideScope");
			WRITE_BYTE(GetScopeType());
		MessageEnd();
	}
}

#endif